package com.czkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czkt.entity.*;
import com.czkt.entity.vo.CstCustomerBasDict;
import com.czkt.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
public class CstCustomerController {
    @Resource
    ICstCustomerService cstCustomerService;

    @Resource
    ICstLinkmanService cstLinkmanService;

    @Resource
    ICstActivityService cstActivityService;

    @Resource
    ICstServiceService cstServiceService;

    @Resource
    ICstLostService cstLostService;

    @Resource
    IBasDictService basDictService;

    @Resource
    ISysUserService sysUserService;

    /**
     * 营销机会管理
     * @param custName 客户名称
     * @param custRegion 地区
     * @param pageIndex 当前页数
     * @param pageSize 每页记录数
     * @param model 传值
     * @return 跳转到营销机会列表
     */
    @RequestMapping("/customer/list")
    public String customerShow(@RequestParam(value = "custName",defaultValue = "") String custName,
                               @RequestParam(value = "custRegion",defaultValue = "") String custRegion,
                               @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                               @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        List<BasDict> cstCustomerBasDicts = basDictService.selectByDictType();
        IPage<CstCustomerBasDict> customerBasDictIPage = cstCustomerService.cusTomerBas(custName,custRegion,pageIndex,pageSize);
        model.addAttribute("page",customerBasDictIPage);
        model.addAttribute("dictList",cstCustomerBasDicts);
        model.addAttribute("custName",custName);
        model.addAttribute("custRegion",custRegion);
        return "/customer/list";
    }

    /**
     * 删除
     * @param custNo 客户编号
     * @return 删除成功刷新列表
     */
    @RequestMapping("/customer/del/{custNo}")
    @ResponseBody
    public Object serviceUpFenDeal(@PathVariable("custNo") String custNo){
        //根据主表id查询从表是否有主表的数据如果有将其删除
        QueryWrapper<CstLinkman> linkmanQueryWrapper = new QueryWrapper<>();
        linkmanQueryWrapper.lambda().eq(CstLinkman::getLkmCustNo,custNo);
        if(cstLinkmanService.getOne(linkmanQueryWrapper) != null){
            cstLinkmanService.remove(linkmanQueryWrapper);
        }

        QueryWrapper<CstActivity> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.lambda().eq(CstActivity::getAtvCustNo,custNo);
        if(cstActivityService.getOne(activityQueryWrapper) != null){
            cstActivityService.remove(activityQueryWrapper);
        }

        QueryWrapper<CstService> serviceQueryWrapper = new QueryWrapper<>();
        serviceQueryWrapper.lambda().eq(CstService::getSvrCustNo,custNo);
        if(cstServiceService.getOne(serviceQueryWrapper) != null){
            cstServiceService.remove(serviceQueryWrapper);
        }

        QueryWrapper<CstLost> lostQueryWrapper = new QueryWrapper<>();
        lostQueryWrapper.lambda().eq(CstLost::getLstCustNo,custNo);
        if(cstLostService.getOne(lostQueryWrapper) != null){
            cstLostService.remove(lostQueryWrapper);
        }
        return cstCustomerService.removeById(custNo);
    }

    /**
     * 编辑
     * @param custNo 客户编号
     * @param model 传值
     * @return 跳转到修改页面
     */
    @RequestMapping("/customer/edit")
    public String cstCustomerController(String custNo,Model model){
        model.addAttribute("customer",cstCustomerService.getById(custNo));
        model.addAttribute("address",basDictService.selectByDictType());
        model.addAttribute("dicts",basDictService.selectByDict());
        model.addAttribute("users",sysUserService.list());
        return "/customer/edit";
    }

    /**
     * 修改
     * @param cstCustomer 营销机会实体类
     * @return 修改成功跳转到营销机会列表
     */
    @RequestMapping("/customer/save")
    public String customerSave(CstCustomer cstCustomer){
        if(cstCustomerService.updateById(cstCustomer)){
            return "redirect:/customer/list";
        }else {
            return "forward:/customer/edit?custNo=" + cstCustomer.getCustNo();
        }
    }

//    /**
//     *
//     * @param custName
//     * @return
//     */
//    @RequestMapping("/customer/name/{custName}")
//    @ResponseBody
//    public Object customerCustName(@PathVariable("custName") String custName){
//        boolean fig = false;
//        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(custName!=null,CstCustomer::getCustName,custName);
//        CstCustomer cstCustomer = cstCustomerService.getOne(queryWrapper);
//        if (cstCustomer == null){
//            fig = true;
//        }
//        return fig;
//    }
}
